/*
 * Powered By 
 * Since 2010 - 2016
 */

package com.rf.business.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.sys.utils.PageUtils;
import com.rf.sys.utils.ConfigUtils;
import com.rf.sys.dao.SqlAdapterMapper;
import com.rf.business.dao.TravelLogMapper;
import com.rf.business.model.TravelLog;
import com.rf.business.model.TravelLogExample;
import com.rf.business.service.TravelLogService;

@Service("travelLogService")
public class TravelLogServiceImpl implements TravelLogService {
	
	@Autowired
   	private SqlAdapterMapper sqlAdapterMapper;//注入dao
   	@Autowired
   	private TravelLogMapper travelLogMapper;//注入dao

   	/**
	 * 根据id获取数据
	 * @param travelLogId
	 * @return TravelLog
	 */
   	@Override
	public TravelLog getTravelLogByID(String id
) throws Exception{
		// TODO Auto-generated method stub
		return travelLogMapper.selectByPrimaryKey(id
);
	}
	
	/**
	 * 根据TravelLog获取数据
	 * @param TravelLog
	 * @return List<TravelLog>
	 */
	@Override
	public List<TravelLog> getTravelLog(TravelLog travelLog) throws Exception{
		// TODO Auto-generated method stub
		TravelLogExample example = new TravelLogExample();
		com.rf.business.model.TravelLogExample.Criteria criteria= example.createCriteria();
		
		//criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//不查询已删除的
		List<TravelLog> list = travelLogMapper.selectByExample(example);
		return list;
	}
	
	/**
	 * 获取所有数据
	 * @param 
	 * @return List<TravelLog>
	 */
	@Override
	public List<TravelLog> getTravelLog() throws Exception{
		// TODO Auto-generated method stub
		TravelLogExample example = new TravelLogExample();
		com.rf.business.model.TravelLogExample.Criteria criteria= example.createCriteria();
		
		//criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//不查询已删除的
		return travelLogMapper.selectByExample(example);
	}
   	
	/**
	 * 保存信息
	 * @param TravelLog
	 * @return
	 */
	@Override
	public void addTravelLog(TravelLog travelLog) throws Exception{
		// TODO Auto-generated method stub
		travelLogMapper.insert(travelLog);
	}

	/**
	 * 修改数据
	 * @param TravelLog
	 * @param
	 */
	@Override
	public void updateTravelLog(TravelLog travelLog) throws Exception{
		// TODO Auto-generated method stub
		travelLogMapper.updateByPrimaryKeySelective(travelLog);
	}
	
	/**
	 * 删除数据
	 * @param travelLogId
	 * @param
	 */
	@Override
	public void deleteTravelLog(String id
) throws Exception{
		// TODO Auto-generated method stub
		travelLogMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 获取数据分页
	 * @param TravelLog
	 * @param pageUtils
	 * @return Map
	 */
	@Override
	public Map getTravelLogPage(TravelLog travelLog,PageUtils pageUtils) throws Exception{
		TravelLogExample example = new TravelLogExample();
		com.rf.business.model.TravelLogExample.Criteria criteria= example.createCriteria();
		
		//获取第offset页，每页共limit条内容，默认查询总数count
		Page p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getRows());
		//下一条执行语句，即为要被转换的语句
		List<TravelLog> list = travelLogMapper.selectByExample(example);
		Map dateMap = new HashMap();
	    dateMap.put("total", p.getTotal());
	    dateMap.put("rows", list);
		return dateMap;
	}
	/**
	 * 旅行社日志查询
	 */
	public Map getTravelLogsPage(TravelLog travelLog,PageUtils pageUtils) throws Exception{
		StringBuffer sql=new StringBuffer("select (select t.name from bus_department t where t.id=g.tra_id) TRA_ID_NAME,g.*,(select r.user_name from sys_user r where r.id=g.MEET_CAR_USER) CREATE_USER_NAME　from bus_travel_log g ");
		Page p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getRows());
		List list = sqlAdapterMapper.selectSQL(sql.toString());
		Map dateMap = new HashMap();
	    dateMap.put("total", p.getTotal());
	    dateMap.put("rows", list);
		return dateMap;
	}
}

